package com.coin.exchanges.view.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.coin.exchanges.R;
import com.coin.exchanges.cache.PreferenceManager;
import com.coin.exchanges.model.okex.request.UserRegisterReq;
import com.coin.exchanges.model.okex.response.FuturesRateRes;
import com.coin.exchanges.model.okex.response.UserRegisterRes;
import com.coin.exchanges.net.RetrofitFactory;
import com.coin.exchanges.utils.ClearEditText;
import com.coin.exchanges.utils.LCSharedPreferencesHelper;
import com.coin.exchanges.view.MainActivity;
import com.coin.exchanges.view.user.bean.UserRegisterBean;
import com.coin.libbase.view.activity.JToolbarActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RegisterActivity extends JToolbarActivity {


    @BindView(R.id.mRegisterLogoIv)
    ImageView mRegisterLogoIv;
    @BindView(R.id.mRegisterPhoneEt)
    ClearEditText mRegisterPhoneEt;
    @BindView(R.id.mRegisterPasswordEt)
    ClearEditText mRegisterPasswordEt;
    @BindView(R.id.mRegisterLl)
    LinearLayout mRegisterLl;
    @BindView(R.id.mRegisterTwoPasswordEt)
    ClearEditText mRegisterTwoPasswordEt;
    @BindView(R.id.mRegisterTwoLl)
    LinearLayout mRegisterTwoLl;
    @BindView(R.id.mRegisterNickEt)
    ClearEditText mRegisterNickEt;
    @BindView(R.id.mRegisterBtn)
    Button mRegisterBtn;
    @BindView(R.id.mUserLoginMatchLl)
    LinearLayout mUserLoginMatchLl;

    protected int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initIntent(Intent intent) {

    }

    @Override
    protected void initView() {
        setToolbarBackground(R.color.white);
        setTitle("注册");
    }

    @Override
    protected void initData() {

    }


    @OnClick({R.id.mRegisterBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mRegisterBtn:
                String mobileNum = mRegisterPhoneEt.getText().toString().trim();
                String password = mRegisterPasswordEt.getText().toString().trim();
                String passwordTwo = mRegisterTwoPasswordEt.getText().toString().trim();
                String nickname = mRegisterNickEt.getText().toString().trim();

                if(registerCheckAll(mobileNum,password,passwordTwo,nickname)){
                    PostNetRegister(mobileNum,password,passwordTwo,nickname);
                }


                break;
            default:
                break;
        }
    }

    private void PostNetRegister(String mobileNum,
                                 String password,
                                 String passwordTwo,
                                 String nickname ) {
        Map<String, String> map = new HashMap<>();
        map.put("phone",mobileNum);
        map.put("password",password);
        map.put("password_1",passwordTwo);
        map.put("username",nickname);
               RetrofitFactory
                .getOkExApiService()
                .register(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserRegisterRes>() {


                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserRegisterRes userRegisterRes) {
                        if(userRegisterRes.getResult()==1){
                            LCSharedPreferencesHelper sharedPreferencesHelper = LCSharedPreferencesHelper.instance(RegisterActivity.this,"register");
                            sharedPreferencesHelper.putBoolean("isLogin", true);
                            sharedPreferencesHelper.putValue("UserId",userRegisterRes.getData().get(0).UserId);
                            sharedPreferencesHelper.putValue("UserName",userRegisterRes.getData().get(0).UserName);
                            sharedPreferencesHelper.putValue("UserPhone",userRegisterRes.getData().get(0).UserPhone);
                            sharedPreferencesHelper.putValue("UserMoney",userRegisterRes.getData().get(0).UserMoney.toString());
                            startActivity(new Intent(RegisterActivity.this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
                            Toast.makeText(RegisterActivity.this,"恭喜你注册成功",Toast.LENGTH_LONG).show();
                        }else {
                            Toast.makeText(RegisterActivity.this,userRegisterRes.getReturnval(),Toast.LENGTH_LONG).show();
                        }


                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    private Boolean registerCheckAll(String mobileNum,
                                     String password,
                                     String passwordTwo,
                                     String nickname ) {


        if(TextUtils.isEmpty(mobileNum)){
            Toast.makeText(this,"请输入手机号",Toast.LENGTH_LONG).show();
            return false;
        }
        if(TextUtils.isEmpty(password)){
            Toast.makeText(this,"请输入密码",Toast.LENGTH_LONG).show();
            return false;
        }
        if(TextUtils.isEmpty(passwordTwo)){
            Toast.makeText(this,"请确认密码",Toast.LENGTH_LONG).show();
            return false;
        }
        if(!password.equals(passwordTwo)){
            Toast.makeText(this,"请确认两次输入的密码",Toast.LENGTH_LONG).show();
            return false;
        }
        if(TextUtils.isEmpty(nickname)){
            Toast.makeText(this,"请输入昵称",Toast.LENGTH_LONG).show();
            return false;
        }

        return true;
    }
}
